package com.taurus.myboard.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.taurus.myboard.common.vo.Dict;
import com.taurus.myboard.entity.Job;
import com.taurus.myboard.mapper.JobMapper;
import com.taurus.myboard.pojo.JobVo;
import com.taurus.myboard.service.IJobService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taurus.myboard.utils.HttpClientPool;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kanten
 * @since 2023-06-16
 */
@Service
//@Slf4j
public class JobServiceImpl extends ServiceImpl<JobMapper, Job> implements IJobService {

    @Resource
    HttpClientPool httpClientPool = new HttpClientPool();

    @Value("${fateflow.url}")
    private String url;

    @Override
    public Map<String, Object> getJobList(String role, String partyId, String status, String note,
                                          Long pageNo, Long pageSize) {
        JSONObject jobParams = new JSONObject();
        jobParams.put("limit", -1);
        String rst = httpClientPool.post(url + Dict.URL_LIST_JOB, JSON.toJSONString(jobParams));
        Map<String, Object> data = new HashMap<>();
        JSONObject jo = JSON.parseObject(rst);
        JSONArray jd = jo.getJSONArray(Dict.DATA);
        List<Map> infos = JSON.parseArray(jd.toJSONString(), Map.class);
        List<JobVo> jobVos = infos.stream()
//                .sorted((h1, h2) -> ((Integer)h2.get("f_job_id")).compareTo((Integer)h1.get("f_job_id")))
                .sorted(Comparator.comparing(h -> ((String)h.get("f_job_id"))))
                .filter(x -> (StringUtils.hasLength(role) ? role.equalsIgnoreCase((String) x.get("f_role")) : true)
                    && (StringUtils.hasLength(partyId) ? partyId.equalsIgnoreCase((String) x.get("f_party_id")) : true)
                    && (StringUtils.hasLength(status) ? status.equalsIgnoreCase((String) x.get("f_status")) : true))
                .map(info -> new JobVo((String) info.get("f_job_id"), (String) info.get("f_role"),
                        (String) info.get("f_party_id"), (String) info.get("f_initiator_party_id"),
                        (String) info.get("f_start_date"), (String) info.get("f_end_date"),
                        info.get("f_elapsed") == null ? "" :
                                DateUtil.secondToTime((Integer) info.get("f_elapsed")/1000),
                        (String)info.get("f_status")))
            .collect(Collectors.toList());
        jobVos.sort(Comparator.comparing(JobVo::getId).reversed().thenComparing(JobVo::getRole));
        //split page
        List<JobVo> subJobList = jobVos.stream().skip((pageNo-1)*pageSize).limit(pageSize).
                collect(Collectors.toList());
        data.put("total", jobVos.size());
        data.put("rows", subJobList);
        return data;
    }
}
